<?php
    session_start();
    include_once('connect.php');
    class shopping_cart{
        var $_SESSION = array();
        function add_item($pro_id,$pro_name,$pro_price_sell){
            $_SESSION['product'][] = array(
                'pro_id'=>$pro_id,
                'pro_name'=>$pro_name,
                'pro_price_sell'=>$pro_price_sell
            );
            
        }
        function clear_one_item($item){
            unset($_SESSION['product'][$item]);
        }
        function clear_all_item(){
            unset($_SESSION['product']);
        }
        function destroy_cart(){
            session_destroy();
        }
        function unset_cart(){
            unset($_SESSION);
        }
        function implode(){
            implode(',',$_SESSION['product']);
        }
        
    }
    $cart = new shopping_cart;
    if(isset($_POST['add_item'])){
        $cart->add_item('2','san pham 2','20');
    }
    if(isset($_POST['delete'])){
        $cart->clear_one_item($_POST['delete']);
    }
    if(isset($_POST['delete_all_item'])){
        $cart->clear_all_item();
    }
    if(isset($_POST['session_destroy'])){
        $cart->destroy_cart();
    }
    if(isset($_POST['implode'])){
        $cart->implode  ();
    }
    if(isset($_POST['for_each'])){
        $cart->for_each();
    }
    
    
//    $tong = 0;
//    foreach($_SESSION['product'] as $key => $product2){
//        $tong = $tong + $product2['pro_price_sell'];
//    };
//    $arr['user'] = array(
//        'user_id'=>'1',
//        'user_name'=>'nguyen duc dinh'
//    );
    //unset($_SESSION['product'][2])
    //echo('<pre>');
//    print_r($_SESSION);
    //session_destroy();
    //echo $tong;

?>
<form name="pro_item" action="" method="POST">
<table border = '1px' style="background: #00ff00;">
    <tr>
        <td>STT</td>
        <td>product ID</td>
        <td>Product Name</td>
        <td>Price Sell</td>
    </tr>
<?php
    foreach($_SESSION['product'] as $pro_item => $value){
?>
    <tr>
        <td><input type="submit" id="<?php echo $pro_item;?>" name="delete" value="<?php echo $pro_item;?>" style="font-size: 0px; width: 70px;height: 25px;display: block; margin: 5px;" /></td>
        <td><?php echo $value['pro_id'];?></td>
        <td><?php echo $value['pro_name'];?></td>
        <td><?php echo $value['pro_price_sell'];?></td>
<?php
    
    }

?>

</table>

    <input type="submit" name="add_item" id="add_item" value="Add Item" />
    <input type="submit" name="delete_one_item" value="delete one item" />
    <input type="submit" name="delete_all_item" value="delete all item" />
    <input type="submit" name="session_destroy" value="session destroy" />
    <input type="submit" name="implode" value="implode" />
    <input type="submit" name="for_each" value="for_each" />
</form>











